//顺时针打印矩阵
//1  2  3  4 100
//5  6  7  8 200
//9 10  11 12 500
//13 14 15 16 300
public class Print2DArray {
    public static void main(String[] args)
    {
        int[][] arr=new int[][]{{1,2,3,4,100},{5,6,7,8,200},{9,10,11,12,500},{13,14,15,16,300}};
        int colStr=0,rowStr=0;//作为边界
        int rowEnd=arr.length-1,colEnd=arr[0].length-1;//作为边界
        while(colStr<=colEnd&&rowStr<=rowEnd) {
            int c=colStr,r=rowStr; //进行变化打印
            //打印上面一行
            while (c <= colEnd) {
                System.out.print(arr[r][c++] + " ");
            }
            //恢复
            c = colEnd;
            r ++;
            //打印右边一列
            while (r <= rowEnd)
                System.out.print(arr[r++][c] + " ");
            //恢复
            r=rowEnd;
            c--;
            //打印下面一条边
            while (c >= colStr) {
                System.out.print(arr[r][c--] + " ");
            }
            //恢复
            c=colStr;
            r--;
            //左边一条边
            while (r > rowStr) {
                System.out.print(arr[r--][c] + " ");
            }
            colEnd--;
            colStr++;
            rowEnd--;
            rowStr++;
        }
    }
}
